package behavioral_pattern.memento_pattern;

/**
 * 备忘录
 */
public class MainMemento {
    static MementoCaretaker mementoCaretaker = new MementoCaretaker();
    private static int index = -1; //记录当前状态所在位置
    public static void main(String[] args) {

       Chessman chessman = new Chessman("车",1,1);
      play(chessman);
      chessman.setX(4);
      play(chessman);
      chessman.setY(10);

      undo(chessman,index);
      undo(chessman,index);
      redo(chessman,index);
      redo(chessman,index);

    }

    public static void display(Chessman chessman){
        System.out.println("棋子:"+chessman.getLabel()+"，当前位置："+"第"+chessman.getX()+"行"+"第"+chessman.getX()+"列");
    }

    /**
     * 下棋
     */
    public static void play(Chessman chessman){
        mementoCaretaker.setMemento(chessman.save());
        index++;
        System.out.println("play->index"+index);
        display(chessman);
    }
    /**
     * 悔棋
     */
    public static void undo(Chessman chessman,int i){
        System.out.println("*****悔棋******");

        index--;
        System.out.println("undo->index"+index);
        chessman.restore(mementoCaretaker.getMemento(i));
        display(chessman);
    }
    /**
     * 撤销悔棋
     */
    public static void redo(Chessman chessman ,int i){
        System.out.println("*****撤销悔棋****");
        index ++;
        System.out.println("redo->index"+index);
        chessman.restore(mementoCaretaker.getMemento(i+1));
        display(chessman);
    }
}
